package com.example.interceptor;

import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 登录拦截器
 */
public class LoginInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 判断请求头中是否有token, 如果没有token,则直接拒绝访问
        String token = request.getHeader("token");
        if(StringUtils.isEmpty(token)){
            throw new RuntimeException("请先登录再操作" + request.getRequestURI());
        }
        return true; // true表示放行, false表示不放行
    }
}
